<?php
//smarty扩展静态页面生成

class MySmarty extends Smarty 
{
	private $renew;						//是否用静态页面,0为不用，1为用，2为更新
	private $pagepath;						//文件存放路径,绝对路径
	private $pageurl;						//文件相对位置
	private $pagetime=0;					//有效时间,0为无限时间
	private $pagebuild=0;					//是否要重建页面
	private $pagesrc;						//原文件名(带.htm),display的页面
	private $pageid;						//display的页面id
	private $pagename;						//静态文件全名
	private $module_path;						//所在模块
	private	$tpl_path='template';			//模板文件目录
	private	$html_path='html';			//静态文件目录
	private $file_count = '500';			//每目录文件数
	

	function setModuleDir($module){
		 $this->module_path = $module;
		 $this->tpl_path = $module.'template/';
		 $this->html_path = $module.'html/';
	}
	
	//页面格式 news_10.htm  filename_id.htm
	function mypage($filename,$id='',$renew=0,$time='7200',$pathfolder='',$newfilename='') {
		//$this->pagesrc = $this->tpl_path.'/'.$filename.'.htm';
		
		$this->pagesrc = $this->tpl_path.$filename.'.htm';
		
		$this->pageid = $id;

		if(intval($id)>0){
			$pathfolder .=$pathfolder ? '/'.$this->get_folder_name(intval($id)) : $this->get_folder_name(intval($id));
		}
		//$this->pagename = $pagename;

		//是否要静态化
		if(!$renew){
			//不重建页面
			$this->pagebuild = 0;	
		}else{
			if(!$filename)die("You should set the display file!");
			//目录是不存在
			$path = $this->html_path.$pathfolder;
			if(!is_dir($path)){
				mkdir($path, 0777);
				@chmod($path, 0777);
			}

			//是否有id代号
			if(trim($id)) {
				$this->pagename  = $filename.'_'.$id.'.htm' ;
			}else{
				$this->pagename  = $filename.'.htm';
			}
			
			//如果指定新文件名
			if($newfilename){
				$this->pagename = $newfilename;
			}

			
			if($pathfolder){
				$this->pageurl = $this->html_path.$pathfolder.'/'.$this->pagename ;
				$this->pagepath = $path.'/';
			}
			else{
				$this->pageurl = $this->html_path.$this->pagename ;
				$this->pagepath = $path;
			}
				

			$thefile = $this->pagepath.$this->pagename ;
			
			$this->pagetime = intval($time);	
			//判断页面是否存在，同时是否过期,否则重建页面
			
			if(intval($renew) == '2'){
				$this->pagebuild = 1;
			}else{
				
				if(file_exists($thefile)){
					
					//过期重建
					if( (time()-filemtime($thefile)) > intval($time) and $this->pagetime != '0' ){
						$this->pagebuild = 1;
					}else{
						//echo $this->pageurl;
						//不过期,并存在则跳转,并停止运行下面的程序
						$this->display($this->pageurl,$this->pageid);
						exit;
					}
				}else{
					$this->pagebuild = 1;
				}
			}
			return $this->pagebuild;
		}
	}
	
	//重建原函数
	function mydisplay($filename='',$fileid=''){
		if(trim($filename)){
			$this->pagesrc = $filename ;
		}
		if($fileid){
			$this->pageid = $fileid ;
		}
		//重建文件,然后跳转
		if($this->pagebuild){
			
			$data = $this->fetch($this->pagesrc,$this->pageid);
			$file = fopen($this->pagepath.$this->pagename, 'w');
			fwrite($file, $data); 
			fclose($file);
			/*//echo "<script language='javascript'>window.location.href='$this->pageurl'</script>";*/
			$this->display($this->pageurl);
		}else{
			//echo $this->pagesrc;
			$this->display($this->pagesrc,$this->pageid);

		}
                exit;
	}

	//控制目录文件数
	function get_folder_name($id){
		$result = $id-$this->file_count;
		$count = 1;
		while($result>0){
			$count++;
			$result -=$this->file_count;
		}
		return $count*$this->file_count;
	}
}

//使用说明
/*每个页面前数据库操作前用mypage()
再用mydisplay()替换display,可以不加参数,因为mypage()已经设了
*/
?>
